

export function ScrollMore(ref) {
    let loadTimer = null;
    if (ref) {
        ref.addEventListener('scroll', (e) => {
            const { clientHeight, scrollHeight, scrollTop } = e.target;
            const isBottom = scrollTop + clientHeight + 200 > scrollHeight;

            if (isBottom) {
                if (loadTimer) {
                    clearTimeout(loadTimer);
                    loadTimer = setTimeout(() => {
                        // todo............
                    })
                }
            } else {
                loadTimer = loadTimer = setTimeout(() => {
                    // todo............
                })
            }
        });
    }
}

//节流
export function throllet(fn) {
    let timeout = 0;
    const TIME = 3000;
    //节流时间 3秒
    return function () {
        let nowTime = new Date().getTime();
        if (nowTime - timeout > TIME) {
            fn();
            timeout = nowTime;
        } else {

        }
    }
}

//防抖
export function debounce(fn, wait = 100) {
    let timeout = null;
    return function (...args) {
        if (timeout) {
            clearTimeout(timeout)
        }

        timeout = setTimeout(() => {
            fn(...args);
        }, wait);

    }
}


//数组排序

//冒泡排序

//获取URL参数
export function getURL() {


    let searchParam = new URLSearchParams(window.location.href);

    // http://xxxxxxx.com?name=violet&age=18
    console.log(searchParam.get('name')); // violet

    let obj = {
        name: 'violet',
        age: 18
    }

    let toString = new URLSearchParams(obj).toString();
    let url = "http://xxxxx.com";

    console.log(url + '?' + toString); // http://xxxxx.com?name=violet&age=18

}




//Promise


//实现异步控制并发数




// 发布订阅模式

//下载
export function download(url, name = 'a') {
    const param = {
        method: 'GET',
        responseTye: 'blob',
        headers: {
            "Content-Type": 'application/json'
        }
    }
    fetch(url, param).then((res) => {
        return res.blob();
    })
        .then((data) => {

            let blob = new Blob([data]);
            if ("msSaveOrOpenBlob" in navigator) {
                window.navigator.msSaveOrOpenBlob(blob, name);
            } else {
                let aLink = document.createElement('a');
                aLink.download = name;
                aLink.style.display = 'none';
                aLink.href = URL.createObjectURL(blob);
                document.body.appendChild(aLink)
                aLink.click();
                document.body.removeChild(aLink)

            }

        })
        .catch(() => {

        });
}